package com.aaron.dataactions;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletContext;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.struts2.ServletActionContext;

import com.aaron.bean.Error;
import com.aaron.bean.User;
import com.google.gson.Gson;
import com.opensymphony.xwork2.ActionSupport;

public class MobileDataAction extends ActionSupport {

	/**
	 * 
	 */
	ServletRequest request;
	ServletResponse response;
	ServletContext con;
	private static final long serialVersionUID = -8568572282738542059L;

	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		init();
		
		String param1=request.getParameter("p1");
		String param2=request.getParameter("p2");
		User user=new User();
		
	 if(param1==null||param2==null){
			user.setUsername("aaron");
			user.setPassword("aaronno");
			Gson gson=new Gson();
			PrintWriter p=response.getWriter();
			p.print(gson.toJson(user));
			p.close();
		}else{
			user.setUsername(param1);
			user.setPassword(param2);
			Gson gson=new Gson();
			PrintWriter p=response.getWriter();
			p.print(gson.toJson(user));
			p.close();
		}
		return super.execute();
	}

	private void init() {
		request = ServletActionContext.getRequest();
		response = ServletActionContext.getResponse();
		con = ServletActionContext.getServletContext();
		
		response.setCharacterEncoding("UTF-8");
	}
	
	private void error(ServletResponse response) throws IOException{
		Error error=new Error();
		error.setErrorCode(-1);
		error.setType("参数错误!");
		Gson gson=new Gson();
		PrintWriter p=response.getWriter();
		p.print(gson.toJson(error));
		p.close();
	}
	
	

}
